package de.grosjohann.android.feuerwehr;

import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import de.grosjohann.android.feuerwehr.adapter.FahrzeugListAdapter;
import de.grosjohann.android.feuerwehr.domain.Fahrzeug;
import de.grosjohann.android.feuerwehr.notification.Notify;
import de.grosjohann.android.feuerwehr.panel.Panel;
import de.grosjohann.android.feuerwehr.service.FMSDisplayService;
import de.grosjohann.android.feuerwehr.service.IntentServiceFacade;
import de.grosjohann.android.feuerwehr.util.JSON2ObjectUtil;

public class FMSActivity
  extends Activity {

  public static final String FMS_OBJECT = "feuerwehr.FMS_OBJECT";
  public static final String FMS_AUTHORIZED = "feuerwehr.FMS_AUTHORIZED";

  private static final String NAME = "FMSActivity";

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean authorized = false;

    setContentView(R.layout.fms);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
      authorized = extras.getBoolean(FMS_AUTHORIZED);
    }

    ListView lv = (ListView) findViewById(R.id.lvFMS);
    BaseAdapter baseAdapter = new FahrzeugListAdapter(this, R.layout.fahrzeug, authorized);
    lv.setAdapter(baseAdapter);
    lv.setDividerHeight(1);

    lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Fahrzeug obj = ((FahrzeugListAdapter) parent.getAdapter()).getItem(position);
        Intent li = new Intent(FMSActivity.this, StatusActivity.class);
        li.putExtra(FMS_OBJECT, obj);
        startActivity(li);
      }
    });

    // Setting for FMSService
    FMSDisplayService.updateUIListener = new IUpdateUIListener() {
      @Override
      public void updateUI(final String newData) {
        runOnUiThread(new Runnable() {
          @Override
          public void run() {
            updateFMSList(newData);
          }
        });
      }

      @Override
      public void showInfo(final String info) {
        runOnUiThread(new Runnable() {
          @Override
          public void run() {
            Panel panel = new Panel(getApplicationContext(), FMSActivity.this);
            panel.showInfo(info);
          }
        });
      }

      @Override
      public void showWarning(final String warning) {
        runOnUiThread(new Runnable() {
          @Override
          public void run() {
            Panel panel = new Panel(getApplicationContext(), FMSActivity.this);
            panel.showWarning(warning);
          }
        });
      }

      @Override
      public void showError(final String error) {
        runOnUiThread(new Runnable() {
          @Override
          public void run() {
            Panel panel = new Panel(getApplicationContext(), FMSActivity.this);
            panel.showError(error);
          }
        });
      }
    };
  }

  @Override
  protected void onResume() {
    super.onResume();
    Notify.getInstance().clear(this);

    IntentServiceFacade.startOnce(this, FMSDisplayService.class);
  }

  /*
   * Update of FMSData.
   */
  private void updateFMSList(String newData) {
    try {
      ListView lv = (ListView) findViewById(R.id.lvFMS);
      ((FahrzeugListAdapter) lv.getAdapter()).addAll(JSON2ObjectUtil.getFahrzeugList(newData));

      TextView tvNoStatus = (TextView) findViewById(R.id.tvNoStatus);
      tvNoStatus.setVisibility(View.GONE);
    } catch (JSONException ex) {
      Log.e(NAME, ex.getMessage(), ex);

      Panel panel = new Panel(getApplicationContext(), this);
      panel.showError(R.string.errReceiveFMSData);
    }
  }

}
